// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using System.Drawing;
using System.IO;
using Mono.Unix;
using NLog;

namespace wxGadu
{

	/// <summary>
	/// Version of <c>wx.HtmlWindow</c> which allow to 
	/// copy selected text, by using command in context menu
	/// </summary>
	public class CopyHtmlWindow : wx.HtmlWindow
	{
		#region Id
		public enum Id
		{
			copyItem
		}
		#endregion
		
		#region controls
		private wx.Menu contextMenu;
		private wx.MenuItem copyItem;
		#endregion
		
		#region data members
		protected MainFrame mainFrame;
		#endregion
		
		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="parent">
		/// A <see cref="wx.Window"/>
		/// </param>
		/// <param name="id">
		/// A <see cref="System.Int32"/>
		/// </param>
		/// <param name="pos">
		/// A <see cref="Point"/>
		/// </param>
		/// <param name="size">
		/// A <see cref="Size"/>
		/// </param>
		/// <param name="style">
		/// A <see cref="System.UInt32"/>
		/// </param>
		/// <param name="mainFrame">
		/// A <see cref="MainFrame"/>
		/// </param>
		public CopyHtmlWindow(wx.Window parent, int id, Point pos, Size size, uint style, MainFrame mainFrame):
			base(parent, id, pos, size, style)
		{
			this.mainFrame = mainFrame;
			
			EVT_RIGHT_DOWN(new wx.EventListener(OnRightDown));
			EVT_UPDATE_UI((int) Id.copyItem, new wx.EventListener(OnCopyItemUpdateUI));
			EVT_MENU((int) Id.copyItem, new wx.EventListener(OnCopyMenu));
		}
		
		protected void OnRightDown(object sender, wx.Event evt)
		{
			if (contextMenu == null)
			{
				contextMenu = new wx.Menu();
				copyItem = new wx.MenuItem(contextMenu, (int) Id.copyItem, Catalog.GetString("Copy"));
				copyItem.Bitmap = mainFrame.ImageMgr[ImageManager.COPY];
				contextMenu.Append(copyItem);
			}
			
			this.PopupMenu(contextMenu);
		}
		
		protected void OnCopyItemUpdateUI(object sender, wx.Event evt)
		{
			wx.UpdateUIEvent updateCtrl = (wx.UpdateUIEvent) evt;
			
			if (copyItem != null)
			{
				if (this.SelectionText.Length > 0)
					copyItem.Enabled = true;
				else
					copyItem.Enabled = false;
			}
			evt.Skip();
		}
		
		protected void OnCopyMenu(object sender, wx.Event evt)
		{
			string copyText = this.SelectionText;
			wx.Clipboard clipboard = new wx.Clipboard();
			if (!clipboard.Open())
			{
				wx.MessageDialog md = new wx.MessageDialog(this, string.Format("Cannot copy, other application is using clipboard"), 
				                                           Catalog.GetString("Copy error"),
			                                         	  wx.MessageDialog.wxOK|wx.MessageDialog.wxICON_EXCLAMATION);
				md.ShowModal();   
			}
			else
			{
				clipboard.SetData(new wx.TextDataObject(copyText));
				clipboard.Close();
			}
		}
		
	
	}
}
